﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Xml;
using SMSSnew.smss_web_reference;
using System.Collections;
using SMSSnew.BIZ;
using SMSSnew.CLASSES;

namespace SMSSnew.DATA.CLASSES
{
     
    public class timeTableCLZ
    {
        public string class_id;
        public string grade_id;
        public string[,] input_records;


        DataTable dt;

        static smss_web_service_app srv = new smss_web_service_app();
        static readXML XMLreader = new readXML();
        //static readXML XMLreader = new readXML();

        TimetableBIZ ttBIZ = new TimetableBIZ();



        public timeTableCLZ()
        {
        }
        public timeTableCLZ(String c)
        {
            this.class_id = c;
        }

        public void setClassID(String CID)
        {

            this.class_id = CID;
        }

        public void setGradeID(String GID)
        {

            this.grade_id = GID;
        }

        public void setInputRecords(String CID, String GID)
        {
            input_records = getInputRecords(CID, GID);

        }


        public String[,] getInputRecords()
        {
            return input_records;

        }



        //...........................METHOD TO CREATE Timetable..................................
        //orderedRecodrs 2D array takes the ordered records with respect to the subject weight
        // orderedRecords[# of subjects,(teacher_id,subject_code,class_id,grade_id,total_slots)]


        public void createTimetable(string[,] orderedRecords)
        {
            String gradeID, classID, subjectCode, slotID, teacherID;    

            int slot, j, i, m;                                     //j,i,m are loop control variables  
            int index = 0;                                         // index = index of the orderedRecords array
            int p = Convert.ToInt32(orderedRecords[index, 4]);     // p = # of periods to be assigned for a paticular subject

            ArrayList conflict_slot = new ArrayList();


            for (j = 1; j < 9; j++)                                // j-> 1 to 8, timeslots for a day (outer most for loop)
            {
                for (i = 1; i < 6; i++)                            // i-> 1 to 6, days for a week
                {

                    if (p != 0)                                    // p = # of periods to be assigned  
                    {
                        subjectCode = orderedRecords[index, 1];
                        gradeID = orderedRecords[index, 3];
                        classID = orderedRecords[index, 2];
                        teacherID = orderedRecords[index, 0];

                        if (conflict_slot.Count != 0)               //if conflict_slot is not empty
                        {
                            for (m = 0; m <= conflict_slot.Count; m++)
                            {
                                if ((conflict_slot.Count == 0) || (p == 0)) // p = # of periods to be assigned
                                    break;                                  //breaks the loop if conflict_slot is empty or if no of periods to be assigned equals to zero

                                slot = (int)conflict_slot[0];
                                slotID = slot.ToString();

                                if ((ttBIZ.hasTeacherConflict(teacherID, slotID) == false)) // if there is no teacher conflict
                                {
                                    recordInsert(gradeID, classID, slotID, subjectCode, teacherID);
                                    p = p - 1;                       // p = # of periods to be assigned
                                    conflict_slot.Remove(slot);
                                    m = m - 1;
                                    continue;
                                }

                                else                                // if there is a teacher conflict, continue the loop 
                                    continue;

                            }                                       // end of the for loop

                            if (p == 0)             // when loop breaked due to the p==0,takes the next element of the orderedRecordsArray
                            {
                                index = index + 1;
                                subjectCode = orderedRecords[index, 1];
                                gradeID = orderedRecords[index, 3];
                                classID = orderedRecords[index, 2];
                                teacherID = orderedRecords[index, 0];
                                p = Convert.ToInt32(orderedRecords[index, 4]);      // p = # of periods to be assigned
                            }

                        }                       //end of the if (conflict_slot.Count != 0)condition


                        slot = 8 * i - 7 + (j - 1);     //generates the slot id

                        if (hasMoreThanOneSubjectTypes(subjectCode, gradeID) == true)    //check for subjects that have more than one subject types
                         
                        {
                            if (Aesthetic_already_assigned(classID, subjectCode) == true || p != Convert.ToInt32(orderedRecords[index, 4]))    // check whether the aesthetic is already assigned or not
                            {
                                bool b;

                                b = Aesthetic_clash(slot.ToString(), classID, subjectCode);
                                if (b == true)      //if there is no clash allocate the slot
                                {
                                    Aesthetic(slot.ToString(), classID, subjectCode);
                                    p = p - 1;                                  // p = # of periods to be assigned
                                }
                                else                    //if there is a clash
                                {
                                    conflict_slot.Add(slot);
                                    continue;
                                }

                            }

                            else                            // if the selected subject is already assigned
                                p = 0;                     // p = # of periods to be assigned


                        }       //end of the  if (hasMoreThanOneSubjectTypes(subjectCode, gradeID) == true) condition

                        else    //if the subject has only one subject type
                        {
                            slotID = slot.ToString();

                            if ((ttBIZ.hasClassConflict(classID, slotID) == true))      //check for a class conflict 
                                continue;

                            if ((ttBIZ.hasTeacherConflict(teacherID, slotID) == true))  //check for a teacher conflict 
                            {
                                conflict_slot.Add(slot);
                                continue;
                            }

                            recordInsert(gradeID, classID, slotID, subjectCode, teacherID);
                            p = p - 1;                                                  // p = # of periods to be assigned

                        }       // end of the (subjectCode != "S9") condition


                    }           // End of the  if (p != 0)  condition



                    else        //TAKES THE NEW ELEMENT OF THE ORDEREDRECORDS ARRAY
                    {

                        if (index < orderedRecords.GetLength(0))
                        {
                            index = index + 1;
                            subjectCode = orderedRecords[index, 1];
                            gradeID = orderedRecords[index, 3];
                            classID = orderedRecords[index, 2];
                            teacherID = orderedRecords[index, 0];

                            p = Convert.ToInt32(orderedRecords[index, 4]);          // p = # of periods to be assigned

                            if (conflict_slot.Count != 0)                           //if conflict_slot is not empty
                            {
                                for (m = 0; m <= conflict_slot.Count; m++)
                                {
                                    if ((conflict_slot.Count == 0) || (p == 0))     //breaks the loop if conflict_slot is empty or if no of periods to be assigned equals to zero     
                                        break;

                                    slot = (int)conflict_slot[0];
                                    slotID = slot.ToString();

                                    if ((ttBIZ.hasTeacherConflict(teacherID, slotID) == false)) // if there is no teacher conflict
                                    {

                                        recordInsert(gradeID, classID, slotID, subjectCode, teacherID);
                                        p = p - 1;                                     // p = # of periods to be assigned
                                        conflict_slot.Remove(slot);
                                        m = m - 1;
                                        continue;
                                    }
                                    else        // if there is a teacher conflict, continue the loop 
                                        continue;

                                }  //end of the for loop
                                if (p == 0)                                         // when loop breaked due to the p==0,takes the next element of the orderedRecordsArray
                                {
                                    index = index + 1;
                                    subjectCode = orderedRecords[index, 1];
                                    gradeID = orderedRecords[index, 3];
                                    classID = orderedRecords[index, 2];
                                    teacherID = orderedRecords[index, 0];
                                    p = Convert.ToInt32(orderedRecords[index, 4]);  // p = # of periods to be assigned
                                }
                            }       //end of the if (conflict_slot.Count != 0) condition


                            slot = 8 * i - 7 + (j - 1);

                            if (hasMoreThanOneSubjectTypes(subjectCode, gradeID) == true) //check for subjects that have more than one subject types
                         
                            {

                                if (Aesthetic_already_assigned(classID, subjectCode) == true || p != Convert.ToInt32(orderedRecords[index, 4]))
                                {

                                    if (Aesthetic_clash(slot.ToString(), classID, subjectCode) == true)
                                    {
                                        Aesthetic(slot.ToString(), classID, subjectCode);
                                        p = p - 1;                                  // p = # of periods to be assigned
                                    }
                                    else
                                    {

                                        conflict_slot.Add(slot);
                                        continue;
                                    }

                                }
                                else                                                //if the selected subject is already assigned
                                    p = 0;                                          // p = # of periods to be assigned


                            }       //end of the if (hasMoreThanOneSubjectTypes(subjectCode, gradeID) == true) condition
                            else
                            {

                                slotID = slot.ToString();

                                if ((ttBIZ.hasClassConflict(classID, slotID) == true))
                                    continue;

                                if ((ttBIZ.hasTeacherConflict(teacherID, slotID) == true))
                                {
                                    conflict_slot.Add(slot);
                                    continue;
                                }

                                recordInsert(gradeID, classID, slotID, subjectCode, teacherID);
                                p = p - 1;

                            }       //end of the if (subjectCode != "S9") condition

                        }           // end of the   if (index < orderedRecords.GetLength(0))  condition

                    }               // end of the taking new element of the array condition


                }                   //end of the foor loop that represents the days of a week

            }                       //end of the outer most for loop that represents the time slots for a day


            //when the execution comes to the out of the outer most foor loop there may be elements in the conflict_slot,that means not allocate all the periods. So allocate those rest periods under bellow condition
            if (((orderedRecords.GetLength(0) - 1 != index) && (conflict_slot.Count != 0) && (p != 0)) || ((orderedRecords.GetLength(0) - 1 != index) && (conflict_slot.Count != 0) && (p == 0)) || ((orderedRecords.GetLength(0) - 1 == index) && (conflict_slot.Count != 0) && (p != 0)))
            {

                for (i = index + 1; i < orderedRecords.GetLength(0); i++)
                {
                    int restPeriods = p;
                    int x = 0;          // x - index of the conflict_slot array

                    if (x > conflict_slot.Count - 1)
                        break;

                    if (restPeriods != 0)
                        i = index;

                    subjectCode = orderedRecords[i, 1];
                    gradeID = orderedRecords[index, 3];
                    classID = orderedRecords[i, 2];
                    teacherID = orderedRecords[i, 0];

                    if (restPeriods == 0)
                        p = Convert.ToInt32(orderedRecords[i, 4]);              // if restPeriods equqls to zero, takes the no of periods to be assigned
                    else
                        p = restPeriods;                                        // if restPeriods not equqls to zero, takes the no of periods to be assigned

                    while (p != 0)
                    {

                        slot = (int)conflict_slot[x];                           //takes the slot from the conflict_slot array
                        slotID = slot.ToString();

                        //  (ttBIZ.hasTeacherConflict(teacherID, slotID)  -  returns "true" if there is a teacher conflict, returns "false" if there is no teacher conflict
                        //  (ttBIZ.isSelfStudies(teacherID) -  returns "true" if it is a self study subject, returns "false" if it is not a self study subject

                        if (((ttBIZ.hasTeacherConflict(teacherID, slotID) == false)) || (((ttBIZ.hasTeacherConflict(teacherID, slotID) == true)) && (ttBIZ.isSelfStudies(teacherID) == true)))
                        {
                            recordInsert(gradeID, classID, slotID, subjectCode, teacherID);
                            p = p - 1;                                          // p = # of periods to be assigned for a paticular subject
                            conflict_slot.Remove(slot);
                        }
                        else                                                    // if there is a teacher conflict or can't allocate the above taken slot
                        {
                            if (x < conflict_slot.Count - 1)
                                x = x + 1;                                      // increment the index by 1 to take the next element of the conflict_slot_array
                            else                                                // if the index goes to the out of bound break from the while loop
                            {
                                p = 0;                                         // p = # of periods to be assigned
                                break;
                            }
                        }          //end of the  if there is a teacher conflict or can't allocate slot condition
                    }              //end of the while loop

                }                  //end of the for loop 

            }                     //end of the  if (((orderedRecords.GetLength(0) - 1 != index) && (conflict_slot.Count != 0) && (p != 0)) || ((orderedRecords.GetLength(0) - 1 != index) && (conflict_slot.Count != 0) && (p == 0)) || ((orderedRecords.GetLength(0) - 1 == index) && (conflict_slot.Count != 0) && (p != 0))) condition
             
        }                         //end of a createTimetable method




        //...........................METHOD TO allocate the slots for a class and a teacher.................................
        public void recordInsert(String gradeID, String classID, String slotID, String subjectCode, String teacherID)
        {

            string query1 = "", query2 = "";
            try
            {

                query1 = XMLreader.readXMLtxt("InsertToSlotAllocation", "~/DATA/XML/TimetableXML.xml"); //insert timeslots to slotAllocation table

                query1 = query1.Replace("@class_id", classID);
                query1 = query1.Replace("@slot_id", slotID);
                query1 = query1.Replace("@subject_code", subjectCode);
                srv.Modify(query1);

                query2 = XMLreader.readXMLtxt("InsertToTeacherAssignedToSlot", "~/DATA/XML/TimetableXML.xml");  //insert timeslots to teacherAssignedToSlot table

                query2 = query2.Replace("@teacher_id", teacherID);
                query2 = query2.Replace("@class_id", classID);
                query2 = query2.Replace("@slot_id", slotID);
                srv.Modify(query2);

            }
            catch (Exception ex)
            {
                throw ex;
            }


        }


        //...........................METHOD TO check whether is there a teacher conflict or not.................................
        public DataTable teacherConflict(string teacherID, string slotID)
        {
            string query = "";
            DataTable dt = new DataTable();
            try
            {

                query = XMLreader.readXMLtxt("GetTeacherConflict", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@teacher_id", teacherID);
                query = query.Replace("@slot_id", slotID);

                dt = srv.Search(query);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        //...........................METHOD TO check whether is there a class conflict or not.................................
        public DataTable classConflict(string classID, string slotID)
        {
            string query = "";
            DataTable dt = new DataTable();
            try
            {

                query = XMLreader.readXMLtxt("GetClassConflict", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@class_id", classID);
                query = query.Replace("@slot_id", slotID);

                dt = srv.Search(query);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


       

       

       

        


        // for avoid clash aesthetic subjects
        public bool Aesthetic_clash(string timeslot, string classid, string subjectid)
        {
            DataTable dt;
            DataTable dt1;

            string query1 = "", query2 = "", query3 = "";

            try
            {
                
                query1 = XMLreader.readXMLtxt("GetSubjectType", "~/DATA/XML/TimetableXML.xml");
                query1 = query1.Replace("@subject", subjectid);
                dt = srv.Search(query1);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //find all teachers teach aestheic subject for relevant class_id
                    query2 = XMLreader.readXMLtxt("GetAestheticTeachers", "~/DATA/XML/TimetableXML.xml");
                    query2 = query2.Replace("@sub1", Convert.ToString(dt.Rows[i][0]));
                    query2 = query2.Replace("@class_id", classid);
                    dt1 = srv.Search(query2);
                    
                    //check above selected teachers are free in the selected slot
                    query3 = XMLreader.readXMLtxt("GetAestheticTeachersClash", "~/DATA/XML/TimetableXML.xml");
                    query3 = query3.Replace("@teacher", Convert.ToString(dt1.Rows[0][0]));
                  
                    query3 = query3.Replace("@slot", timeslot);

                    DataTable dt2 = srv.Search(query3);

                    //if no of rows in the datatable >0 it means there is a clash for some teacher

                    if (dt2.Rows.Count > 0)
                    {
                        return false;
                        //break;
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return true;

        }


        //for insert aesthetic subjects to slot allocation table and teacher_assigned_to_slot table
        public void Aesthetic(string timeslot, string classid, string subject_id)
        {
            DataTable dt;
            DataTable dt1;

            string query1 = "", query2 = "", query3 = "", query4 = "", query5 = "", query6 = "";
            try
            {

                //insert aesthetic periods for all classes in the same group

                query1 = XMLreader.readXMLtxt("GetSubjectGroups", "~/DATA/XML/TimetableXML.xml");
                query1 = query1.Replace("@class_id", classid);
                query1 = query1.Replace("@subject_id", subject_id);
                dt = srv.Search(query1);
                string groupid = Convert.ToString(dt.Rows[0][0]);

                query3 = XMLreader.readXMLtxt("GetGradeID", "~/DATA/XML/TimetableXML.xml");
                query3 = query3.Replace("@class_id", classid);
                DataTable dt2 = srv.Search(query3);

                query2 = XMLreader.readXMLtxt("GetSubjectGroupClasses", "~/DATA/XML/TimetableXML.xml");
                query2 = query2.Replace("@group_id", groupid);
                query2 = query2.Replace("@subject_id", subject_id);
                query2 = query2.Replace("@grade_id", Convert.ToString(dt2.Rows[0][0]));
                dt1 = srv.Search(query2);




                for (int i = 0; i < dt1.Rows.Count; i++)
                {

                    query4 = XMLreader.readXMLtxt("InsertToSlotAllocation", "~/DATA/XML/TimetableXML.xml");
                    query4 = query4.Replace("@grade_id", Convert.ToString(dt2.Rows[0][0]));
                    query4 = query4.Replace("@class_id", Convert.ToString(dt1.Rows[i][0]));
                    query4 = query4.Replace("@slot_id", timeslot);
                    query4 = query4.Replace("@subject_code", subject_id);



                    srv.Modify(query4);

                }
                query5 = XMLreader.readXMLtxt("query6", "~/DATA/XML/TimetableXML.xml");
                query5 = query5.Replace("@class_id", classid);
                query5 = query5.Replace("@subject_id", subject_id);


                DataTable dt4 = srv.Search(query5);

                //insert all classes in same gruop, to all teachers who teach aesthetic for that group

                for (int i = 0; i < dt4.Rows.Count; i++)
                {
                    for (int j = 0; j < dt1.Rows.Count; j++)
                    {
                        query6 = XMLreader.readXMLtxt("InsertToTeacherAssignedToSlot", "~/DATA/XML/TimetableXML.xml");
                        query6 = query6.Replace("@teacher_id", Convert.ToString(dt4.Rows[i][0]));
                        query6 = query6.Replace("@class_id", Convert.ToString(dt1.Rows[j][0]));
                        query6 = query6.Replace("@slot_id", timeslot);

                        srv.Modify(query6);
                    }
                }

            }

            catch (Exception Exception)
            {

                throw Exception;
            }

        }


        //to check whether aesthetic is allocate for paticular class
        public bool Aesthetic_already_assigned(string classid, string subject_id)
        {

            DataTable dt1;
            string query = "";
            //DataTable dt2;
            //DataTable dt3;
            //DataTable dt4;
            // string assigned="no";

            try
            {
                query = XMLreader.readXMLtxt("GetAlreadyAssignedAesthetic", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@class_id", classid);
                query = query.Replace("@subject_id", subject_id);
                dt1 = srv.Search(query);
                //string groupid = Convert.ToString(dt1.Rows[0][0]);

                if (dt1.Rows.Count > 0)
                {
                    return false;
                }
                return true;
            }

            catch (Exception ex)
            {
                throw ex;
            }

        }




        //...........................METHOD TO take an ordered records array .................................
        public String[,] getInputRecords(string classID, string gradeID)
        {
            int i, j;

            string query = "";
            String[,] table;
            DataTable dt = new DataTable();
            try
            {

                query = XMLreader.readXMLtxt("GetOrderedInputRecords", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@cID", classID);
                query = query.Replace("@gID", gradeID);
                dt = srv.Search(query);

                table = new String[dt.Rows.Count, 5];

                for (i = 0; i < dt.Rows.Count; i++)
                {
                    for (j = 0; j < 5; j++)
                    {
                        table[i, j] = dt.Rows[i][j].ToString();


                    }

                }


            }
            catch (Exception ex)
            {
                throw ex;
            }
            return table;

        }


        // ..........................................Loaded To  Interfaces.............................................

        public DataTable loadGrade()    //load grade IDs to combo box
        {
            string query = "";
            try
            {

                query = XMLreader.readXMLtxt("loadGrade", "~/DATA/XML/TimetableXML.xml");
                dt = srv.Search(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }



        //..............check whether is there a already created time table or not...................................................


        public bool checkForDuplicateClzTimetableCreation(string classID)
        {
            string query = "";
            DataTable dt = new DataTable();
            int totalAllocatedSlots;
            bool verify = false;
                        

            try
            {

                query = XMLreader.readXMLtxt("CheckForDuplicateClzTimetableCreation", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@classID", classID);
                dt = srv.Search(query);

                totalAllocatedSlots = int.Parse(dt.Rows[0][0].ToString());
                if (totalAllocatedSlots == 40)
                    verify = true;
                
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return verify;
            
        }


        //..........method to get number of subjects for a particular class...................................

        public int takeNumberOfSubjects(string classID)
        {
            string query = "";
            DataTable dt = new DataTable();
            int numberOfSubjets;

            try
            {

                query = XMLreader.readXMLtxt("GetNumberOfSubjects", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@classID", classID);
                dt = srv.Search(query);

                numberOfSubjets = int.Parse(dt.Rows[0][0].ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return numberOfSubjets;

        }


        //..........method to get number of allocated teachers for subjects in a particular class...................................

        public int takeNumberOfAllocatedTeachers(string classID)
        {
            string query = "";
            DataTable dt = new DataTable();
            int numberOfTeachers;

            try
            {

                query = XMLreader.readXMLtxt("GetNumberOfAllocatedTeachers", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@classID", classID);
                dt = srv.Search(query);

                numberOfTeachers = int.Parse(dt.Rows[0][0].ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return numberOfTeachers;

        }

        //..................method to check whether the subject has more than one subject type or not................................

        public bool hasMoreThanOneSubjectTypes(string subjectCode, string gradeID)
        {
            string query = "";
            DataTable dt = new DataTable();
            int numberOfSubjectTypes;
            bool verify = false;

            try
            {

                query = XMLreader.readXMLtxt("GetNumberOfSubjectTypes", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@gradeID", gradeID);
                query = query.Replace("@subjectCode", subjectCode);
                dt = srv.Search(query);

                numberOfSubjectTypes = int.Parse(dt.Rows[0][0].ToString());

                if (numberOfSubjectTypes > 1)
                    verify = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return verify;

        }



        // ..........................................Loaded To  Interfaces.............................................

        public DataTable loadClassID(classCLZ objClass) //load class IDs to combo box
        {
            string query = "";
            try
            {

                query = XMLreader.readXMLtxt("loadClassID", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@gradeID", objClass.grade_id);

                dt = srv.Search(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // ..........................................VIEW CLASS TIMETABLE.............................................


        public void viewClassTimetable(string class_id)
        {

            string query = "";
            try
            {
                int mon = 1;
                int tue = 9;
                int wed = 17;
                int thu = 25;
                int fri = 33;

                query = XMLreader.readXMLtxt("create_temporary", "~/DATA/XML/timetableXML.xml");
                srv.Modify(query);

                for (int i = 0; i <= 7; i++)
                {
                    srv = new smss_web_service_app();


                    query = XMLreader.readXMLtxt("insert_temporary", "~/DATA/XML/timetableXML.xml");

                    query = query.Replace("@class_id", class_id);
                    query = query.Replace("@mon", mon.ToString());
                    query = query.Replace("@tue", tue.ToString());
                    query = query.Replace("@wed", wed.ToString());
                    query = query.Replace("@thu", thu.ToString());
                    query = query.Replace("@fri", fri.ToString());


                    srv.Modify(query);

                    mon = mon + 1;
                    tue = tue + 1;
                    wed = wed + 1;
                    thu = thu + 1;
                    fri = fri + 1;


                }


            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public DataTable loadClassTimetableToGrid()
        {
            dt = new DataTable();

            string query = "";

            query = XMLreader.readXMLtxt("loadClassTimetable", "~/DATA/XML/timetableXML.xml");
            dt = srv.Search(query);

            query = XMLreader.readXMLtxt("drop_temporary", "~/DATA/XML/timetableXML.xml");
            srv.Modify(query);

            return dt;




        }




        // ..........................................VIEW TEACHER TIMETABLE.............................................

       
       //this method want to view teacher timetable in a grid
        public DataTable viewTeacherTimetable(string teacher_id)
        {
            XMLreader = new readXML();
            srv = new smss_web_service_app();
            string query1 = "";
            string query2 = "";
            string query3 = "";
            string query4 = "";
            //string query5 = "";
            // string query6 = "";
            string query7 = "";
            string query8 = "";




            try
            {

                query1 = XMLreader.readXMLtxt("createTemporyTable1", "~/DATA/XML/timetableXML.xml");
                srv.Modify(query1);
                query2 = XMLreader.readXMLtxt("createTemporyTable2", "~/DATA/XML/timetableXML.xml");
                srv.Modify(query2);

                int mon = 1;
                int tue = 9;
                int wed = 17;
                int thu = 25;
                int fri = 33;

                for (int i = 0; i <= 7; i++)
                {
                    srv = new smss_web_service_app();


                    query3 = XMLreader.readXMLtxt("insert_tempory1", "~/DATA/XML/timetableXML.xml");


                    query3 = query3.Replace("@teacher_id", teacher_id);
                    query3 = query3.Replace("@mon", mon.ToString());
                    query3 = query3.Replace("@tue", tue.ToString());
                    query3 = query3.Replace("@wed", wed.ToString());
                    query3 = query3.Replace("@thu", thu.ToString());
                    query3 = query3.Replace("@fri", fri.ToString());


                    srv.Modify(query3);
                    mon = mon + 1;
                    tue = tue + 1;
                    wed = wed + 1;
                    thu = thu + 1;
                    fri = fri + 1;



                }

                mon = 1;
                tue = 9;
                wed = 17;
                thu = 25;
                fri = 33;

                for (int i = 0; i <= 7; i++)
                {
                    srv = new smss_web_service_app();


                    query4 = XMLreader.readXMLtxt("insert_tempory2", "~/DATA/XML/timetableXML.xml");

                    query4 = query4.Replace("@teacher_id", teacher_id);
                    query4 = query4.Replace("@mon", mon.ToString());
                    query4 = query4.Replace("@tue", tue.ToString());
                    query4 = query4.Replace("@wed", wed.ToString());
                    query4 = query4.Replace("@thu", thu.ToString());
                    query4 = query4.Replace("@fri", fri.ToString());


                    srv.Modify(query4);
                    mon = mon + 1;
                    tue = tue + 1;
                    wed = wed + 1;
                    thu = thu + 1;
                    fri = fri + 1;


                }
                srv = new smss_web_service_app();


                query7 = XMLreader.readXMLtxt("tempory_time_table1_datatable", "~/DATA/XML/timetableXML.xml");
                DataTable dt1 = srv.Search(query7);

                query8 = XMLreader.readXMLtxt("tempory_time_table2_datatable", "~/DATA/XML/timetableXML.xml");
                DataTable dt2 = srv.Search(query8);


                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    //DataRow dr = dt3.NewRow();

                    //dt3.Rows.Add(dr);

                    for (int j = 0; j < dt1.Columns.Count; j++)
                    {
                        // DataColumn 

                        string one = dt1.Rows[i][j].ToString();
                        string two = dt2.Rows[i][j].ToString();

                        string result = one + ' ' + ' ' + two;
                        dt1.Rows[i][j] = result;
                        // dr[j] = result;


                    }
                    // dt3.Rows.InsertAt(dr, i);

                }


                //query6 = xmlReader.readXMLtxt("insert_tempory3", "~/DATA/XML/timetableXML.xml");
                //srv.Modify(query6);

                return dt1;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public void loadTimetableToGrid()
        {
            try
            {

                //dt1 = new DataTable();
                XMLreader = new readXML();
                srv = new smss_web_service_app();
                //string query = "";
                string query7 = "";
                string query8 = "";
                //string query9 = "";



                // query = xmlReader.readXMLtxt("loadTeacherTimetable", "~/DATA/XML/timetableXML.xml");
                //dt1= srv.Search(query);
                query7 = XMLreader.readXMLtxt("deleteTemporyTable1", "~/DATA/XML/timetableXML.xml");
                srv.Modify(query7);

                query8 = XMLreader.readXMLtxt("deleteTemporyTable2", "~/DATA/XML/timetableXML.xml");
                srv.Modify(query8);

                //query9 = xmlReader.readXMLtxt("deleteTemporyTable3", "~/DATA/XML/timetableXML.xml");
                //srv.Modify(query9);

                //return dt1;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable loadTeacherIdCbo()
        {
            dt = new DataTable();
            //   XmlReader = new readXML();
            //  srv = new smss_web_service_app();
            string query = "";


            query = XMLreader.readXMLtxt("loadTeacherIds", "~/DATA/XML/timetableXML.xml");
            dt = srv.Search(query);


            return dt;

        }

        

    }

}
